package application.common.cron4j;

import java.util.regex.Pattern;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.cron4j.ITask;
import application.common.render.HttpClientResult;
import application.common.util.HttpUtil;
import application.common.util.ShiroUtil;
import net.sf.json.JSONObject;

public class ConfigTask implements ITask{
	
	private boolean isEnd=true;
	
	public void run() {
		if(isEnd) {
			if(ShiroUtil.getConfig()!=null && !StrKit.isBlank(ShiroUtil.getConfig().getSynURL())) {
				isEnd=false;
				try {
					HttpClientResult httpClientResult=HttpUtil.doGet(String.format("%s/api/cab/getConfig?cabinetId=%s", ShiroUtil.getConfig().getSynURL(),ShiroUtil.getAuth()));
					if(httpClientResult==null) {
						isEnd=true;
						return;
					}
					if(httpClientResult.getCode()!=200) {
						isEnd=true;
						return;	
					}
					String content=httpClientResult.getContent();
					if(StrKit.isBlank(content)) {
						isEnd=true;
						return;	
					}
					JSONObject jsonObject=JSONObject.fromObject(content);
					if(jsonObject==null) {
						isEnd=true;
						return;	
					}
					if(jsonObject.has("code")) {
						int code=jsonObject.getInt("code");
						if(code!=200) {
							isEnd=true;
							return;	
						}
						if(jsonObject.has("data")) {
							String data=jsonObject.getString("data");
							if(StrKit.isBlank(data)) {
								isEnd=true;
								return;	
							}
							JSONObject dataJSONObject=JSONObject.fromObject(data);
							if(dataJSONObject==null ) {
								isEnd=true;
								return;
							}
							if(dataJSONObject.containsKey("copyright") &&!StrKit.equals(ShiroUtil.getConfig().getCopyright(), dataJSONObject.getString("copyright"))) {
								if(!StrKit.isBlank(dataJSONObject.getString("copyright")))
									ShiroUtil.getConfig().setCopyright(dataJSONObject.getString("copyright"));
							}
							if(dataJSONObject.containsKey("name") &&!StrKit.equals(ShiroUtil.getConfig().getName(), dataJSONObject.getString("name"))) {
								if(!StrKit.isBlank(dataJSONObject.getString("name")))
									ShiroUtil.getConfig().setName(dataJSONObject.getString("name"));
							}
							if(dataJSONObject.containsKey("phone") &&!StrKit.equals(ShiroUtil.getConfig().getPhone(), dataJSONObject.getString("phone"))) {
								if(!StrKit.isBlank(dataJSONObject.getString("phone")))
									ShiroUtil.getConfig().setPhone(dataJSONObject.getString("phone"));
							}
							if(dataJSONObject.containsKey("isBiopsy") && !StrKit.isBlank(dataJSONObject.getString("isBiopsy"))) {
								if(isInteger(dataJSONObject.getString("isBiopsy"))) {
									ShiroUtil.getConfig().setIsBiopsy(dataJSONObject.getString("isBiopsy"));
								}
							}
							ShiroUtil.getConfig().update();
							isEnd=true;
						}else {
							isEnd=true;
						}
					}else {
						isEnd=true;
					}
				}catch (Exception e) {
					isEnd=true;
				}
			}
			isEnd=true;
		}
	}
	
	public static boolean isInteger(String str) {  
	    Pattern pattern = Pattern.compile("^[-\\ ]?[\\d]*$");  
	    return pattern.matcher(str).matches();  
	}
	

	public void stop() {
	}

}
